<?php
	class Mod_Lib extends CI_Model
	{
		function Mod_Lib()
		{
			parent::__construct();
		}
		
		function load_all($table, $select, $where, $limit, $offset, $order)
		{
			if (isset($select) && !empty($select))
			{
				$this->db->select($select);
			}
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value) 
				{
					$this->db->where($key, $value);
				}
			}
			if (isset($limit) && !empty($limit))
			{
				if (isset($offset) && !empty($offset))
				{
					$this->db->limit($limit, $offset);
				}
				else
				{
					$this->db->limit($limit);
				}
			}
			if (isset($order) && !empty($order))
			{
				foreach ($order as $key=>$value)
				{
					$this->db->order_by($key, $value);
				}
			}
			return $this->db->get($table)->result_array();
		}
		
		function fetch($table, $select, $where=array(), $where_in=array(), $where_not_in=array(), $limit, $offset, $order=array())
		{
			if (isset($select) && !empty($select))
			{
				$this->db->select($select);
			}
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value) 
				{
					$this->db->where($key, $value);
				}
			}
			if (isset($where_in) && !empty($where_in))
			{
				foreach ($where_in as $key=>$value) 
				{
					$this->db->where_in($key, $value);
				}
			}
			if (isset($where_not_in) && !empty($where_not_in))
			{
				foreach ($where_not_in as $key=>$value) 
				{
					$this->db->where_not_in($key, $value);
				}
			}
			if (isset($limit) && !empty($limit))
			{
				if (isset($offset) && !empty($offset))
				{
					$this->db->limit($limit, $offset);
				}
				else
				{
					$this->db->limit($limit);
				}
			}
			if (isset($order) && !empty($order))
			{
				foreach ($order as $key=>$value)
				{
					$this->db->order_by($key, $value);
				}
			}
			return $this->db->get($table)->result_array();
		}
		
		function insert($table, $data)
		{
			$this->db->insert($table,$data);
		}
		
		function count($table, $where=array())
		{
			$this->db->from($table);
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value)
				{
					$this->db->where($key, $value);
				}
			}
			return $this->db->count_all_results();	
		}
		
		function count_load($table, $where=array(), $where_not_in=array())
		{
			$this->db->from($table);
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value)
				{
					$this->db->where($key, $value);
				}
			}
			if (isset($where_not_in) && !empty($where_not_in))
			{
				foreach ($where_not_in as $key=>$value)
				{
					$this->db->where_not_in($key, $value);
				}
			}
			return $this->db->count_all_results();	
		}
		
		function insert_log($action, $admin_id)
		{		
			$data	=	array(
								'action' 			=> $action,
								'time'				=> time(),
								'ip'				=> $this->input->ip_address(),
								'admin_id'			=> $admin_id
			);
			$this->db->insert('logs', $data);
		}
		
		function detail($table, $where=array())
		{
			foreach ($where as $key=>$value)
			{
				$this->db->where($key, $value);
			}
			return $this->db->get($table)->result_array();
		}
		
		function get_news_relate($table, $where_in)
		{
			
			return $this->db->get($table)->result_array();
		}
		
		function update($table, $where=array(), $data)
		{
			foreach ($where as $key=>$value)
			{
				$this->db->where($key,$value);
			}
			$this->db->update($table,$data);
		}
		
		function join($table, $select, $where, $join=array(), $limit, $offset, $order)
		{
//			$this->db->from($table);
			if (isset($select) && !empty($select))
			{
				$this->db->select("$select");
			}
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value) 
				{
					$this->db->where($key, $value);
				}
			}
			if (isset($join) && !empty($join))
			{
				foreach ($join as $key=>$value) 
				{
					$this->db->join($key, $value);
				}
			}
			if (isset($limit) && !empty($limit))
			{
				if (isset($offset) && !empty($offset))
				{
					$this->db->limit($limit,$offset);
				}
				else
				{
					$this->db->limit($limit);
				}
			}
			if (isset($order) && !empty($order))
			{
				foreach ($order as $key=>$value)
				{
					$this->db->order_by($key, $value);
				}
			}
			return $this->db->get("$table")->result_array();
		}
	
		function join_today($table, $select, $where, $join=array(), $limit, $offset, $order)
		{
			$this->db->from($table);
			if (isset($select) && !empty($select))
			{
				$this->db->select($select);
			}
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value)
				{
					$this->db->where($key, $value);
				}
			}
			if (isset($join) && !empty($join))
			{
				foreach ($join as $key=>$value) 
				{
					$this->db->join($key, $value);
				}
			}
			if (isset($limit) && !empty($limit))
			{
				if (isset($offset) && !empty($offset))
				{
					$this->db->limit($limit, $offset);
				}
				else
				{
					$this->db->limit($limit);
				}
			}
			if (isset($order) && !empty($order))
			{
				foreach ($order as $key=>$value)
				{
					$this->db->order_by($key, $value);
				}
			}
			return $this->db->count_all_results();
		}
	
		function count_search($table, $like=array(), $where=array())
		{
			$this->db->from($table);
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value)
				{
					if ($value != 0)
					{
						$this->db->where($key, $value);
					}
				}
			}
			if (isset($like) && !empty($like))
			{
				foreach ($like as $key=>$value)
				{
					if ($value != 0)
					{
						$this->db->like($key, $value);
					}
				}
			}
			return $this->db->count_all_results();	
		}
	
		function load_all_search($table,$select,$where=array(),$like=array(),$limit,$offset,$order=array())
		{
			if (isset($select) && !empty($select))
			{
				$this->db->select("$select");
			}
			//$this->db->where(1,1);
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key => $value) {
					if ($value != 0)
					{
						$this->db->where($key,$value);
					}
				}
			}
			if (isset($like) && !empty($like))
			{
				foreach ($like as $key => $value) {
					if ($value != 0)
					{
						$this->db->like($key,$value);
					}
				}
			}
			if (isset($limit) && !empty($limit))
			{
				if (isset($offset) && !empty($offset))
				{
					$this->db->limit($limit,$offset);
				}else{
					$this->db->limit($limit);
				}
			}
			if (isset($order) && !empty($order))
			{
				foreach ($order as $key=>$value)
				{
					$this->db->order_by($key,$value);
				}
			}
			return $this->db->get("$table")->result_array();
		}
	
		function count_search_join($table,$like=array(),$where=array(),$join=array())
		{
			$this->db->from($table);
		//	$this->db->where('1','1');
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key=>$value)
				{
					$this->db->where($key,$value);
				}
			}
			if (isset($like) && !empty($like))
			{
				foreach ($like as $key=>$value)
				{
					$this->db->like($key,$value);
				}
			}
			if (isset($join) && !empty($join))
			{
				foreach ($join as $key=>$value)
				{
					$this->db->join($key,$value);
				}
			}
			return $this->db->count_all_results();	
		}
		
		function load_all_search_join($table,$select,$where=array(),$like=array(),$join=array(),$limit,$offset,$order=array())
		{
			if (isset($select) && !empty($select))
			{
				$this->db->select("$select");
			}
			//$this->db->where(1,1);
			if (isset($where) && !empty($where))
			{
				foreach ($where as $key => $value) {
					$this->db->where($key,$value);
				}
			}
			if (isset($like) && !empty($like))
			{
				foreach ($like as $key => $value) {
					$this->db->like($key,$value);
				}
			}
			if (isset($limit) && !empty($limit))
			{
				if (isset($offset) && !empty($offset))
				{
					$this->db->limit($limit,$offset);
				}else{
					$this->db->limit($limit);
				}
			}
			if (isset($order) && !empty($order))
			{
				foreach ($order as $key=>$value)
				{
					$this->db->order_by($key,$value);
				}
			}
			if (isset($join) && !empty($join))
			{
				foreach ($join as $key=>$value)
				{
					$this->db->join($key,$value);
				}
			}
			return $this->db->get("$table")->result_array();
		}
		
		function detail_limit($table,$select,$where=array())
		{
			if (isset($select) && !empty($select))
			{
				$this->db->select("$select");
			}
			foreach ($where as $key=>$value)
			{
				$this->db->where($key,$value);
			}
			return $this->db->get($table)->result_array();
		}
		
		function delete($table, $value, $field, $in=true)
		{
			if($in == true)
			{
				$this->db->where_in($field, $value);
			}
			else
			{
	            $this->db->where($field, $value);
			}
			return $this->db->delete($table);
		}
	}